<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/26 0026
 * Time: 10:51
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\PriceHistory
 *
 * @property int $id
 * @property string $purchase_price
 * @property string $sale_price
 * @property int $product_id
 * @property int $create_time
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PriceHistory newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PriceHistory newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PriceHistory query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PriceHistory whereCreateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PriceHistory whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PriceHistory whereProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PriceHistory wherePurchasePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PriceHistory whereSalePrice($value)
 * @mixin \Eloquent
 */
class PriceHistory extends Model
{
    public $table = "price_history";

    protected $guarded = [];

    public $timestamps = false;

    /**
     * @param $product_id
     * @param $purchase_price
     * @param $sale_price
     */
    public static function addHistory($product_id, $purchase_price, $sale_price)
    {
        PriceHistory::create([
            'product_id' => $product_id,
            'purchase_price' => $purchase_price,
            'sale_price' => $sale_price,
            'create_time' => time()
        ]);
    }

    /**
     * @param  Product $product
     */
    public static function addHistoryByProduct($product)
    {
        PriceHistory::addHistory(
            $product->id,
            $product->purchase_price,
            $product->sale_price);
    }
}
